<template>
  <div id="order" class="container">
    <!-- <div class="search">
      <el-form ref="searchForm" :model="searchForm" inline label-width="90px">
        <el-form-item label="内链名称：" prop="title">
          <el-input v-model="searchForm.title" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search(searchForm)">查询</el-button>
          <el-button @click="reset(searchForm)">重置</el-button>
        </el-form-item>
      </el-form>
    </div> -->
    <h3 class="title"><span />内容模型</h3>
    <el-button type="primary" class="add" size="small" icon="el-icon-plus" style="margin-bottom:15px" @click="goAdd">添加</el-button>
    <BouncedDialog :title="id?'编辑':'添加'" :dialog-show.sync="dialogShow" :width="50" class="link">
      <el-form ref="addForm" :model="addForm" :rules="rules" inline class="addForm" label-width="80px">
        <el-form-item label="名称" prop="title">
          <el-input v-model="addForm.title" />
        </el-form-item>
        <el-form-item label="表名" prop="table">
          <el-input
            v-model="addForm.table"
            :disabled="id?true:false"
            placeholder="表名由字母和下划线组成，必须以字母开头跟结尾，如：table_name"
          /></el-form-item>
        <el-form-item label="模型类型" prop="type">
          <el-select v-model="addForm.type">
            <el-option label="独立模型" :value="1" disabled />
            <el-option label="主表附表模型" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="addForm.description" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" />
        </el-form-item>
        <!-- <el-form-item label="附表" prop="database_table">
          <el-input v-model="addForm.database_table" />
        </el-form-item> -->
        <!-- <el-form-item label="系统模型">
          <el-select v-model="addForm.is_system">
            <el-option label="是" :value="1" />
            <el-option label="否" :value="2" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="状态">
          <el-select v-model="addForm.status">
            <el-option label="正常" :value="1" />
            <el-option label="禁用" :value="2" />
          </el-select>
        </el-form-item>
        <div class="btn">
          <el-button @click="dialogShow = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-form>
    </BouncedDialog>
    <el-table
      :data="orderData"
      border
      stripe
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}"
    >
      <el-table-column label="ID" prop="id" width="60px" />
      <el-table-column label="模型名称" prop="title" />
      <el-table-column label="表名" prop="table" />
      <el-table-column label="附表" prop="database_table" />
      <el-table-column label="模型类型" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type==1">独立模型</span>
          <span v-if="scope.row.type==2">主表附表模型</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="系统模型">
        <template slot-scope="scope">
          <span v-if="scope.row.is_system==1">是</span>
          <span v-if="scope.row.is_system==2">否</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="描述" prop="description" /> -->
      <el-table-column label="状态" width="140px">
        <template slot-scope="scope">
          <!-- <span v-if="scope.row.status==1">正常</span>
          <span v-else-if="scope.row.status==2">禁用</span> -->
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="2"
            active-text="正常"
            inactive-text="禁用"
            active-color="#f84311"
            inactive-color="rgba(0,0,0,0.2)"
            @change="disableUser(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="260px" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" class="orange" size="mini" @click="check(scope.row)">查看模型字段</el-button>
          <el-button class="edit" size="mini" @click="editor(scope.row)">编辑</el-button>
          <el-button class="dele" size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      :hide-on-single-page="page.total<page.size?true:false"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
import moudleApi from '@/api/module.js'
export default {
  data() {
    return {
      orderData: [], //
      dialogShow: false,
      addForm: {
        title: '',
        table: '',
        database_table: '',
        description: '',
        type: 2,
        // is_system: '',
        status: ''
      },
      rules: {
        title: [
          { required: 'true', message: '请输入模型名', trigger: 'blur' }
        ],
        table: [
          { required: 'true', message: '请输入表名', trigger: 'blur' }
        ],
        description: [
          { required: 'true', message: '请输入模型描述', trigger: 'blur' }
        ],
        type: [
          { required: 'true', message: '请选择模型类型', trigger: 'change' }
        ]
      },
      searchForm: {
        title: ''
      },
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      }, // 分页器
      val: {

      },
      id: ''
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    // this.getTikets()
  },
  updated() {

  },
  methods: {
    disableUser(row) {
      const val = {
        title: row.title,
        table: row.table,
        database_table: row.database_table,
        description: row.description,
        type: row.type,
        // is_system: row.is_system,
        id: row.id,
        status: ''
      }
      if (row.status == 1) {
        val.status = 1
      } else {
        val.status = 2
      }

      moudleApi.moduleUpdate(val).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 新增
    goAdd() {
      this.id = ''
      this.addForm = {}
      this.addForm.status = 1
      this.addForm.type = 2
      this.dialogShow = true
    },
    // 编辑
    editor(row) {
      this.id = row.id
      this.addForm = row
      this.dialogShow = true
    },
    submit() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (!this.addForm.id) {
            moudleApi.moduleSave({ ...this.addForm }).then(res => {
              if (res.code == 0) {
                this.$message.success(res.msg)
                this.dialogShow = false
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            moudleApi.moduleUpdate({ ...this.addForm }).then(res => {
              if (res.code == 0) {
                this.$message.success(res.msg)
                this.dialogShow = false
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        } else {
          this.$message.error('请填写必要信息')
        }
      })
    },
    // 查看模型字段列表
    check(row) {
      this.$router.push({ name: 'modelField', query: { id: row.id }})
    },
    // 删除
    del(row) {
      this.$confirm('此操作将永久删除该模型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        moudleApi.moduleDelete({ id: row.id }).then(res => {
          if (res.code == 0) {
            this.$message.success(res.msg)
            this.getList()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 搜索
    search(searchForm) {
      this.$refs.searchForm.validate((valid) => {
        this.page.currentPage = 1
        this.getList(this.val)
      })
    },
    // 重置搜索框
    reset(searchForm) {
      this.searchForm.title = ''
    },
    // 模型列表
    getList() {
      this.val.page = this.page.currentPage
      this.val.limit = this.page.size
      // this.val.title = this.searchForm.title
      moudleApi.moduleList(this.val).then(res => {
        this.orderData = res.data
        this.page.total = res.count
      })
    },

    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getList()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getList()
    }

  }
}
</script>
<style lang='scss'>
#order{
  .link{
    .el-select{
        width: 100%;
    }
  }
  .addForm .el-form-item__content{
      width: calc(100% - 80px);
    }
}
</style>
<style scoped lang='scss'>
    .el-table{
      margin-bottom: 20px;
    }
    .search{
      min-width: 1350px;
      background-color: #fff;
      padding: 35px 30px 20px;
      border-radius: 5px;
    }
    .el-form-item{
      width: 100%;
    }

</style>
